﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;

namespace Invini.Training
{
    public class DecorateAction : TrainingActionBase
    {
        private class Decorator : Adorner
        {
            public Decorator(UIElement element) : base(element) { }
            protected override void OnRender(DrawingContext drawingContext)
            {
                Pen renderPen = new Pen(Brushes.Red, 2.0);
                Rect rect = new Rect(this.AdornedElement.RenderSize);

                double halfPenWidth = renderPen.Thickness / 2.0;

                GuidelineSet guidelines = new GuidelineSet();
                guidelines.GuidelinesX.Add(rect.Left + halfPenWidth);
                guidelines.GuidelinesX.Add(rect.Right + halfPenWidth);
                guidelines.GuidelinesY.Add(rect.Top + halfPenWidth);
                guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth);

                drawingContext.PushGuidelineSet(guidelines);

                // Draw a circle at each corner.
                drawingContext.DrawRectangle(null, renderPen, rect);
            }
        }
        private class ActiveAdorner
        {
            private AdornerLayer layer;
            private Adorner adorner;

            public ActiveAdorner(AdornerLayer layer, Adorner adorner)
            {
                this.layer = layer;
                this.adorner = adorner;
            }
            public void Clear()
            {
                this.layer.Remove(this.adorner);
            }
        }

        public PathComponentsCollection Element
        {
            get;
            set;
        }

        private List<ActiveAdorner> activeAdorners;

        public DecorateAction()
        {
            this.Element = new PathComponentsCollection();
        }
        public override void Execute()
        {
            if (this.Element == null)
            {
                this.onCompleted();
                return;
            }

            this.activeAdorners = new List<ActiveAdorner>();

            IEnumerable<object> objects = this.Element.FindElements();
            if (objects.Count() == 0)
            {
                this.onCompleted();
                return;
            }

            foreach (DependencyObject dpo in objects)
            {
                UIElement element = dpo as UIElement;
                if (element == null)
                    continue;

                AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
                if (layer == null)
                    continue;

                Adorner adorner = new Decorator(element);
                layer.Add(adorner);
                activeAdorners.Add(new ActiveAdorner(layer, adorner));
            }

            this.onCompleted();
       }

        public override void Clear()
        {
            base.Clear();

            if (this.activeAdorners != null)
            {
                foreach (ActiveAdorner activeAdorner in this.activeAdorners)
                    activeAdorner.Clear();

                this.activeAdorners = null;
            }
        }
    }
}
